<?php
/**
 * @ClassName: Upload
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-06 14:56
 * @Version: V1.0.0
 */

namespace app\api\controller;

use think\Controller;

class Upload extends Controller
{
    /**
     * 文件上传
     * @return \think\response\Json|void
     */
    function uploadfile()
    {
        $file = request()->file('file');
        $type = input('type', '');

        $root_path = env("root_path");
        $uploadDir = "/uploads/files";
        if ($type) {
            $uploadDir .= '/' . $type;
        }

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $fileInfo = $file->getInfo();//文件信息
            $originalName = $fileInfo['name'];//原文件名
            $typeArr = explode('/', $fileInfo['type']);//文件类型
            $fileSize = $fileInfo['size'];//文件大小（kb）
            $info = $file->move($root_path . 'public' . $uploadDir);
            if ($info) {
                // 成功上传后 获取上传信息
                $name = $info->getSaveName();

                $name = str_replace("\\", "/", $name);

                $url = $uploadDir . "/" . $name;

                $result = [
                    'url' => $url,
                    'fileType' => $typeArr[0],
                    'originalName' => $originalName,
                    'url2' => replace_url($url),
                ];

                return json(["code" => 1, "msg" => "上传成功", "data" => $result]);
            } else {
                return json(["code" => 0, "msg" => "上传失败"]);
            }
        }
    }

}